/**
 * synchronized修饰普通方法
 */
public class ThreadSynchronized2 {
    private static int number=0;
    static class Counter{
        //循环次数
        private static int MAX_COUNT=1000000;
        //++方法
        public synchronized  void incr(){
            for (int i = 0; i <MAX_COUNT ; i++) {
                number++;
            }
        }
        //--方法
        public synchronized  void decr(){
            for (int i = 0; i <MAX_COUNT ; i++) {
                number--;
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Counter counter=new Counter();
        Thread t1=new Thread(()->{
            counter.incr();
        });
        t1.start();

        Thread t2=new Thread(()->{
            counter.decr();
        });
        t2.start();

        //等待线程执行完成
        t1.join();
        t2.join();
        System.out.println("最终的结果:"+number);
    }
}
